package com.moleq.mgnetwork;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private InetAddress localInetAddress;
	private String localIp;
	private byte[] localIpBytes;
	private Object regBuffer;
	private WifiManager wifiManager;
	private EditText etIp;
	private EditText etFilePath;
	private NetworkMonitor nwMonitor;

	private TextView tvMyIp;
	private MultiCommunication multicomm;
	private TextView textViewLog;
	private Button btnExit;
	private Intent mMainServiceIntent;
	private OnClickListener onClickListener = new OnClickListener() {

		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.btnExit:
				finish();
				break;

			default:
				break;
			}

		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// load my ip
		wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		new CheckNetConnectivity().start();

		mMainServiceIntent = new Intent(this, TransporterService.class);
		startService(mMainServiceIntent);

		tvMyIp = (TextView) this.findViewById(R.id.tvMyLoaclationIP);

		btnExit = (Button) this.findViewById(R.id.btnExit);
		btnExit.setOnClickListener(onClickListener);

		Constant.shareContext = Constant.getShareContext(getApplicationContext());

	}

	// 检测网络连接状态,获得本机IP地址
	private class CheckNetConnectivity extends Thread {

		public void run() {
			try {
				if (!wifiManager.isWifiEnabled()) {
					wifiManager.setWifiEnabled(true);
				}

				for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
					NetworkInterface intf = en.nextElement();
					for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
						InetAddress inetAddress = enumIpAddr.nextElement();
						if (!inetAddress.isLoopbackAddress()) {
							if (inetAddress.isReachable(1000)) {
								localInetAddress = inetAddress;
								localIp = inetAddress.getHostAddress().toString();
								localIpBytes = inetAddress.getAddress();
								// System.arraycopy(localIpBytes, 0, regBuffer,
								// 44, 4);

								Message msg = new Message();
								msg.what = Constant.MY_LOACL_IP;
								msg.obj = localIp;
								msgHandler.sendMessage(msg);
							}
						}
					}
				}

			} catch (Exception ex) {
				ex.printStackTrace();
			}
		};
	};

	private final Handler msgHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 1:
				// Toast.makeText(MGCommTest.this, msg.obj.toString().trim(),
				// Toast.LENGTH_LONG).show();
				textViewLog.setText(textViewLog.getText().toString() + "\n" + msg.obj.toString());
				break;
			case Constant.MY_LOACL_IP:
				String localIp = msg.obj.toString().trim();
				tvMyIp.setText(localIp);

				startMonitor();

			default:
				break;
			}
		}
	};

	private void startMonitor() {
		Map<String, Boolean> ipAddressList = new HashMap<String, Boolean>();
		ipAddressList.put("192.168.1.111", false);
		ipAddressList.put("192.168.1.116", false);
		ipAddressList.put("192.168.1.107", false);
		// ipAddressList.put("192.168.1.0", false);

		ipAddressList.remove(localIp);

		// nwMonitor = new NetworkMonitor(ipAddressList , 10,msgHandler);
		// nwMonitor.start();
	}
}
